package API.ExerciseDome;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * ClassName:ExerciseText005
 * Package:API.ExerciseDome
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/2/3-12:58
 * @Version:v1.0
 */
public class ExerciseText005 {
    /*使用代码实现计算你活了多少天,用JDK7和JDK8两种形式*/
    public static void main(String[] args) throws ParseException {
        /*JDK7
         * 规则：只要对时间计算或者判断,都需要先获取当前时间的毫秒值
         * */
        //1.计算出生年月日的毫秒值
        String birthday = "2000年04月04日";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = sdf.parse(birthday);
        long birthdayTime = date.getTime();
        //获取当前时间的毫秒值
        long todayTime = System.currentTimeMillis();
        long time = todayTime - birthdayTime;
        System.out.println(time / 1000 / 60 / 60 / 24);

        /*
        * JDK8
        * Period(年月日)   Duration(时分秒)   ChronoUnit*/
        LocalDate ld1 = LocalDate.of(2000,04,04);
        LocalDate ld2 = LocalDate.now();
        long days = ChronoUnit.DAYS.between(ld1, ld2);
        System.out.println(days);
    }
}
